package com.example.hackergateway.factory;

import com.example.hackergateway.emun.HttpCodeEnum;
import com.example.hackergateway.exception.CommonException;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * Description：
 *
 * @Author: cw
 * CreateTime：2022-10-14
 */
@Component
public class AppGatewayFileterFactory extends AbstractGatewayFilterFactory<AppGatewayFileterFactory> {

    public AppGatewayFileterFactory() {
        super(AppGatewayFileterFactory.class);
    }

    @Override
    public GatewayFilter apply(AppGatewayFileterFactory config) {
        return  new AppGatewayFileterFactory.AppGatewayFileter();
    }

    public class AppGatewayFileter implements GatewayFilter, Ordered {


        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            Object xtoken = exchange.getAttribute("xtoken");
            if(ObjectUtils.isEmpty(xtoken)){
                throw new CommonException(HttpCodeEnum.UNAUTHORIZED.getCode());
            }

//            if(!JwtUtil.checkToken(xtoken.toString())){
////                throw new CommonException(HttpCodeEnum.SIGNATURE_ERROR.getCode());
////            }
            return chain.filter(exchange);
        }

        @Override
        public int getOrder() {
            return -999;
        }
    }

    public static class Config{

    }
}
